package cn.ainit.sys.common.dao;
/***
 * SQL Pagination
 * 2013.11.29
 * @author xuzq
 * 
 */
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import cn.ainit.sys.constant.SystemConstant;
import cn.ainit.sys.constant.TypesConstant;
import cn.ainit.test.web.User;

public class Pagination {
	public static final int NUMBERS_PER_PAGE = 30;
	//一页显示的记录数
	private int numPerPage;
	//记录总数
	private int totalRows;
	 //总页数
	private int totalPages;
	 //当前页码
	private int currentPage = 1;
	 //起始行数
	private int startIndex;
	 //结束行数
	private int lastIndex;
	 //结果集存放List
	private List resultList;
	//数据库类型
	private int dbType;
	 //JdbcTemplate jTemplate
	private JdbcTemplate jdbcTemplate;
	 /**分页构造函数
	 * @param sql 根据传入的sql语句得到一些基本分页信息
	 * @param currentPage 当前页
	 * @param numPerPage 每页记录数
	 * @param jTemplate JdbcTemplate实例
	 */
	 public Pagination(int dbType,String sql,int currentPage,int numPerPage,JdbcTemplate jTemplate){
		 setJdbcTemplate(jTemplate);
		 if(jTemplate == null){
			 throw new IllegalArgumentException("com.deity.ranking.util.Pagination.jTemplate is null,please initial it first. ");
		 }else if(sql == null || sql.equals("")){
			 throw new IllegalArgumentException("com.deity.ranking.util.Pagination.sql is empty,please initial it first. ");
		 }
		 if (currentPage <= 0) currentPage = 1; 
		 if (numPerPage <= 0) numPerPage = 30;
		//设置每页显示记录数
		 setNumPerPage(numPerPage);
		 //设置要显示的页数
		 setCurrentPage(currentPage);
		 //计算总记录数
		 StringBuffer totalSQL = new StringBuffer(" SELECT count(*) FROM ( ");
		 totalSQL.append(sql);
		 totalSQL.append(" ) totalTable ");
		 //给JdbcTemplate赋值
		 setJdbcTemplate(jdbcTemplate);
		 //总记录数
		 setTotalRows(getJdbcTemplate().queryForInt(totalSQL.toString()));
		 //计算总页数
		 setTotalPages();
		 //计算起始行数
		 setStartIndex();
		 //计算结束行数
		 setLastIndex();
		 //构造oracle数据库的分页语句
		 StringBuffer paginationSQL = new StringBuffer("");
		 if (dbType == TypesConstant.DB_ORACLE_TYPE) {
			 paginationSQL.append(" SELECT * FROM ( ");
			 paginationSQL.append(" SELECT temp.* ,ROWNUM num FROM ( ");
			 paginationSQL.append(sql);
			 paginationSQL.append(" ) temp ");
			 paginationSQL.append(" ) WHERE   num <= " + lastIndex+" and 　num > " + startIndex);
		}else if (dbType == TypesConstant.DB_MYSQL_TYPE) {
			paginationSQL.append(sql);
			paginationSQL.append(" limit " + startIndex + "," + lastIndex);
		}
		 //构造mysql的分页语句
		 //装入结果集
		setResultList(getJdbcTemplate().queryForList(paginationSQL.toString()));
	 }
	 
	 public int getCurrentPage() {
		 return currentPage;
	 }
	 public void setCurrentPage(int currentPage) {
		 this.currentPage = currentPage;
	 }
	 public int getNumPerPage() {
		 return numPerPage;
	 }
	 public void setNumPerPage(int numPerPage) {
		 this.numPerPage = numPerPage;
	 }
	 public List getResultList() {
		 return resultList;  
	 }
	 public void setResultList(List resultList) {
		 this.resultList = resultList;
	 }
	 public int getTotalPages() {
		 return totalPages;
	 }
	 //计算总页数
	 public void setTotalPages() {
		 if(totalRows % numPerPage == 0){
			 this.totalPages = totalRows / numPerPage;
		 }else{
			 this.totalPages= (totalRows / numPerPage) + 1;
		 }
	 }
	 
	 public int getTotalRows() {
		 return totalRows;
	 }
	 
	 public void setTotalRows(int totalRows) {
		 this.totalRows = totalRows;
	 }
	 
	 public int getStartIndex() {
		 return startIndex;
	 }
	 
	 public void setStartIndex() {
		 this.startIndex = ((currentPage<1)?1:currentPage - 1) * numPerPage;
	 }
	 
	 public int getLastIndex() {
		 return lastIndex;
	 }
	 public JdbcTemplate getJdbcTemplate() {
		 return jdbcTemplate;
	 }
	 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		 this.jdbcTemplate = jdbcTemplate;
	 }
	 //计算结束时候的索引
	 public void setLastIndex() {
System.out.println("totalRows="+totalRows);///////////
System.out.println("numPerPage="+numPerPage);///////////
		 if( totalRows < numPerPage){
			 this.lastIndex = totalRows;
		 }else if((totalRows % numPerPage == 0) || (totalRows % numPerPage != 0 && currentPage < totalPages)){
			 this.lastIndex = currentPage * numPerPage;
		 }else if(totalRows % numPerPage != 0 && currentPage == totalPages){//最后一页
			 this.lastIndex = totalRows ;
		 }
	 }
	public int getDbType() {
		return dbType;
	}
	public void setDbType(int dbType) {
		this.dbType = dbType;
	}

	public Pagination() {
	}

	public static void main(String[] args) {
		Pagination p = new Pagination();
		p.setCurrentPage(3);
		p.setNumPerPage(300);
		p.setTotalRows(1500);
		p.setTotalPages();
		//计算起始行数
		p.setStartIndex();
		//计算结束行数
		p.setLastIndex();

		System.out.println(p.getLastIndex());
		System.out.println(p.getStartIndex());
	}
}
